package com.gubei.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.gubei.subject.application.convert.SubjectAnswerDTOConverter;
import com.gubei.subject.application.convert.SubjectInfoDTOConverter;
import com.gubei.subject.application.dto.SubjectInfoDTO;
import com.gubei.subject.common.entity.PageResult;
import com.gubei.subject.common.entity.Result;
import com.gubei.subject.domain.entity.SubjectAnswerBO;
import com.gubei.subject.domain.entity.SubjectInfoBO;
import com.gubei.subject.domain.service.SubjectInfoDomainService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/subject")
public class SubjectController {

    @Resource
    private SubjectInfoDomainService subjectInfoDomainService;

    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectInfoDTO subjectInfoDTO){
        try {
            if (log.isInfoEnabled()){
                log.info("SubjectController.add.subjectInfoDTO:{}", JSON.toJSONString(subjectInfoDTO));
            }
            Preconditions.checkArgument(StringUtils.isNotBlank(subjectInfoDTO.getSubjectName()),"題目の名称をnullにすることはできません。");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectDifficult(),"題目の難易度をnullにすることはできません。");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectType(),"題目のタイプをnullにすることはできません。");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectScore(),"題目の点数をnullにすることはできません");
            Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectInfoDTO.getCategoryIdList()),"題目の分類idをnullにすることはできません");
            Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectInfoDTO.getLabelIdList()),"題目のラベルidをnullにすることはできません。");
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertInfoDtoToInfoBo(subjectInfoDTO);
            List<SubjectAnswerBO> subjectAnswerBOList = SubjectAnswerDTOConverter.INSTANCE.convertAnswerDtoListToAnswerBoList(subjectInfoDTO.getOptionList());
            subjectInfoBO.setOptionList(subjectAnswerBOList);
            subjectInfoDomainService.add(subjectInfoBO);
            return Result.ok(true);
        } catch (Exception e) {
            log.error("SubjectController.add.error:{}",e.getMessage(), e);
            return Result.fail("題目の追加に失敗しました。");
        }
    }

    @PostMapping("/getSubjectPage")
    public Result<PageResult<SubjectInfoDTO>> getSubjectPage(@RequestBody SubjectInfoDTO subjectInfoDTO){
        try {
            if (log.isInfoEnabled()){
                log.info("SubjectController.getSubjectPage.subjectInfoDTO:{}", JSON.toJSONString(subjectInfoDTO));
            }
            Preconditions.checkNotNull(subjectInfoDTO.getCategoryId(),"分類idをnullにすることはできません。");
            Preconditions.checkNotNull(subjectInfoDTO.getLabelId(),"ラベルidをnullにすることはできません。");
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertInfoDtoToInfoBo(subjectInfoDTO);
            subjectInfoBO.setPageNo(subjectInfoDTO.getPageNo());
            subjectInfoBO.setPageSize(subjectInfoDTO.getPageSize());
            PageResult<SubjectInfoBO> subjectInfoBOPageResult = subjectInfoDomainService.getSubjectPage(subjectInfoBO);
            return Result.ok(subjectInfoBOPageResult);
        } catch (Exception e) {
            log.error("SubjectController.getSubjectPage.error:{}",e.getMessage(), e);
            return Result.fail("題目リストのクエリに失敗しました");
        }
    }

    @PostMapping("/querySubjectInfo")
    public Result<SubjectInfoDTO> querySubjectInfo(@RequestBody SubjectInfoDTO subjectInfoDTO){
        try{
            if (log.isInfoEnabled()){
                log.info("SubjectController.querySubjectInfo.subjectInfoDTO:{}", JSON.toJSONString(subjectInfoDTO));
            }
            Preconditions.checkNotNull(subjectInfoDTO.getId(),"題目idをnullにすることはできません。");
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertInfoDtoToInfoBo(subjectInfoDTO);
            SubjectInfoBO boResult = subjectInfoDomainService.querySubjectInfo(subjectInfoBO);
            SubjectInfoDTO dtoResult = SubjectInfoDTOConverter.INSTANCE.convertInfoBoToInfoDto(boResult);
            return Result.ok(dtoResult);
        } catch (Exception e) {
            log.error("SubjectController.querySubjectInfo.error:{}",e.getMessage(), e);
            return Result.fail("題目詳細のクエリに失敗しました。");
        }
    }
}
